load('stocks.mat')
load('naivebayesresult_200.mat')
fid = fopen('dictionary.txt','rt');
numTokens = 0;
while (fgets(fid) ~= -1),
    numTokens = numTokens+1;
end
numTokens=25;
path='testfolder';
foldercontent=dir(path);
numDays=size(foldercontent,1)-3;
datestr_start=18;
datestr_length=9;

stocks_status=[];
output=[];
for i=1:numDays
    token_count=zeros(1,numTokens);
    filename=char(foldercontent(i+3).name);
    datestr=filename(datestr_start:datestr_start+datestr_length);
    findindex=0;

    for j=1:length(date)
        if isequal(char(date(j)),datestr)
            findindex=j;
        end
    end
    findindex
    fullfilename=[path '/' char(foldercontent(i+3).name)]

    stocks_status=[stocks_status,y(findindex)];
    fid = fopen(fullfilename,'rt');
    numTweets = 0;
    while (fgets(fid) ~= -1),
        numTweets = numTweets+1;
    end
    fclose(fid);
    testMatrix = textread(fullfilename,'%d', 50*numTweets);
    testMatrix=testMatrix(testMatrix>0);
    testMatrix=testMatrix(testMatrix<=numTokens);
%     testMatrix=testMatrix(testMatrix~=31);
%     testMatrix=testMatrix(testMatrix~=81);
%     testMatrix=testMatrix(testMatrix~=31);
%     testMatrix=testMatrix(testMatrix~=44);
    

    for j=1:length(testMatrix)
        token_count(testMatrix(j))=token_count(testMatrix(j))+1;
    end
    log_p_up_numerator=log_p_up_prior+sum(token_count.*log_p_token_up);
    log_p_down_numerator=log_p_down_prior+sum(token_count.*log_p_token_down);
    [a,b]=max(abs(token_count.*log_p_token_up))
    log_p_up_numerator-log_p_down_numerator
    output=[output,(log_p_up_numerator>log_p_down_numerator)];
end

error=100*sum(output==stocks_status)/length(output)

plot(1:numDays,stocks_status,1:numDays,output);
legend('True Prediction','Our Algortihm')

save('alg_out_training.mat','stocks_status','output','error')
% numTweets = size(testMatrix, 1);
% %---------------
% % YOUR CODE HERE
% for i=1:numTweets
%     k=1;
%     while (testMatrix(i,k)~=0) && (k<50)
%         token_count(testMatrix(i,k))=token_count(testMatrix(i,k))+1;
%         k=k+1;
%     end
% 
% end


%---------------

% 
% % Compute the error on the test set
% error=0;
% for i=1:numTestDocs
%   if (category(i) ~= output(i))
%     error=error+1;
%   end
% end
% 
% %Print out the classification error on the test set
% error/numTestDocs


